<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>websoket</title>
</head>
<body>
    <input type="text" id="message">
    <button id="submit" onclick = send()>提交</button>
    <script>
        //创建websocket实例
        let websoket = new WebSocket("ws://127.0.0.1:8080/test");//这里的test对应后端代码中注册时设置的test
        //给实例挂载回调函数
        websoket.onopen = function() {
            console.log("建立连接");
        }
        websoket.onmessage = function(e) {
            console.log("收到消息" + e.data);
        }
        websoket.onerror = function() {
            console.log("连接异常");
        }
        websoket.onclose = function() {
            console.log("连接关闭");
        }
        function send() {
            websoket.send(document.getElementById("message").value)
        }
    </script>
</body>
</html>